package com.trtan.hr.service;

import com.trtan.hr.dao.MenuDao;
import com.trtan.hr.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: trtan
 * @create: 2021-03-06 13:14
 * @description:
 **/
@Service
public class MenuService{
    @Autowired
    MenuDao menuDao;

    public List<Menu> getAllMenus() {
        return menuDao.getAllMenus();
    }

    @Transactional(rollbackFor = Exception.class)
    public RespPageEntity getAllMenus(Integer page, Integer size, String menuName) {
        RespPageEntity entity = new RespPageEntity();
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Menu> menus = menuDao.getAllMenusInfo(page, size, menuName);
        entity.setData(menus);
        Long total = menuDao.getTotal(menuName);
        entity.setTotal(total);
        return entity;
    }

    public List<Menu> getMenusByHrId(Integer hrId, Integer parentId) {
        return menuDao.getMenusByHrId(hrId, parentId);
    }

    public void addMenu(Menu menu) {
        menuDao.addMenu(menu);
    }

    public void modifyMenu(Menu menu) {
        menuDao.modifyMenu(menu);
    }

    public void deleteMenu(Integer id) {
        menuDao.deleteMenuByMenuId(id);
    }

    public Map<String, Object> getMenuTree(Integer role_id) {
        Map<String, Object> result = new HashMap<>();
        List<Menu> menus = menuDao.getCompleteMenus();
        List<Menu> ownerMenus = menuDao.getMenusByRoleId(role_id);
        System.out.println(ownerMenus.toString());
        Tree tree = new Tree();
        if (menus != null && menus.size() > 0) {
            Menu menu = menus.get(0);
            tree.setId(menu.getId());
            tree.setPid(menu.getParentId());
            tree.setLabel(menu.getName());
            boolean isLeaf = buildMenuTree(tree, menu.getChildren(), ownerMenus, false);
            if (isLeaf) {
                tree = null;
            }
        }
        Tree ownerTree = null;
        if (ownerMenus != null && ownerMenus.size() > 0) {
            ownerTree = new Tree();
            Menu menu = ownerMenus.get(0);
            ownerTree.setId(menu.getId());
            ownerTree.setPid(menu.getParentId());
            ownerTree.setLabel(menu.getName());
            buildMenuTree(ownerTree, menu.getChildren(), ownerMenus, true);
        }
        result.put("fromData", tree);
        result.put("toData", ownerTree);
        return result;
    }

    private boolean buildMenuTree(Tree root, List<Menu> children, List<Menu> ownerMenus, Boolean equal) {
        List<Tree> childrenTrees = new ArrayList<>();
        for (Menu menu : children) {
            Tree tree = new Tree();
            tree.setId(menu.getId());
            tree.setPid(menu.getParentId());
            tree.setLabel(menu.getName());
            //是否是叶子节点
            boolean isLeaf = buildMenuTree(tree, menu.getChildren(), ownerMenus, equal);
            //是否已经授权过了
            boolean exist = checkHasChildren(menu.getId(), ownerMenus);
            if (!equal && exist && isLeaf) {
            } else {
                childrenTrees.add(tree);
            }
        }
        root.setChildren(childrenTrees);
        if (children == null || children.size() == 0 || childrenTrees.size() == 0) {
            return true;
        }
        return false;
    }

    boolean checkHasChildren(Integer menuId, List<Menu> menus) {
        if (menus == null || menus.size() == 0) {
            return false;
        }
        boolean flag = false;
        for (Menu menu : menus) {
            if (menuId.equals(menu.getId())) {
                return true;
            }
            flag |= checkHasChildren(menuId, menu.getChildren());
            if (flag == true) {
                break;
            }
        }
        return flag;
    }


}
